package com.itzxx.huazhun.ui.record.uploaded

import android.R
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.itzxx.huazhun.data.model.EventMessage
import com.itzxx.huazhun.data.model.EventType
import com.itzxx.huazhun.data.model.RecordChildBean
import com.itzxx.huazhun.databinding.FragmentRecordUploadedBinding
import com.itzxx.huazhun.ui.record.RecordFragment
import com.itzxx.huazhun.ui.record.curve.CurveAnalysisActivity
import com.itzxx.huazhun.view.adapter.CustomLoadMoreAdapter
import com.itzxx.library_base_ktx.base.fragment.BaseVmDbFragment
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class RecordUploadedFragment :
    BaseVmDbFragment<RecordUploadedViewModel, FragmentRecordUploadedBinding>() {
    private lateinit var recordAdapter: RecordAdapter
    private lateinit var helper: QuickAdapterHelper
    private lateinit var loadMoreAdapter: CustomLoadMoreAdapter

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        initRecyclerView()
        initListener()
    }

    private fun initListener() {
        mDatabind.btSearchCurve.setOnClickListener {
                mViewModel.onEvaluateEvent(recordAdapter.items)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    private fun initRecyclerView() {
        recordAdapter = RecordAdapter()
        loadMoreAdapter = CustomLoadMoreAdapter()
        loadMoreAdapter.setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener {
            override fun onLoad() {
                // 执行加载更多的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun onFailRetry() {
                // 加载失败后，点击重试的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun isAllowLoading(): Boolean {
                return true
            }
        })
        helper = QuickAdapterHelper.Builder(recordAdapter)
            .setTrailingLoadStateAdapter(loadMoreAdapter)
            .build()
        helper.trailingLoadState = LoadState.None
        mDatabind.rvList.addLinearAdapter(helper.adapter)
        recordAdapter.setOnItemClickListener { adapter, view, position ->
            val item = recordAdapter.getItem(position) as RecordChildBean
            if (TextUtils.isEmpty(item.equipmentId)){
                CurveAnalysisActivity.startActivity(
                    mActivity,
                    item.id.toString(),
                )
            }else{
                CurveAnalysisActivity.startActivity(
                    mActivity,
                    item.equipmentId.toInt(),
                    item.id.toString(),
                    item.equipmentNo
                )
            }
        }

    }

    override fun lazyLoadData() {
        mViewModel.run {
            getList(true)
            querySceneType()
        }
    }

    override fun createObserver() {
        mViewModel.run {
            listDataUiStateLiveData.observe(this@RecordUploadedFragment) {
                if (it.isRefresh) {
                    (parentFragment as RecordFragment).finishRefresh()
                    recordAdapter.submitList(it.listData)
                } else {
                    recordAdapter.addAll(it.listData)
                }

                if (it.hasMore) {
                    helper.trailingLoadState = LoadState.NotLoading(false)
                } else {
                    loadMoreAdapter.setFirstNotFullScreen(it.isRefresh)
                    helper.trailingLoadState = LoadState.NotLoading(true)
                }
            }

            sceneTypeLiveData.observe(this@RecordUploadedFragment) {
                val title = arrayListOf<String>()
                it.forEach {  item->
                    title.add(item.label)
                }
                val adapter = ArrayAdapter(
                    mActivity, R.layout.simple_spinner_item, title.toArray()
                )
                adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
                mDatabind.tvSearchType.adapter = adapter
                mDatabind.tvSearchType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                    override fun onNothingSelected(parent: AdapterView<*>?) {
                        // 可选的回调，一般不需要
                    }

                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                        // 在这里处理点击事件
                        mViewModel.currentCheckSceneType = position
                    }
                }
            }
        }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    //接收事件
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    fun onReceiveMsg(message: EventMessage) {
        if (EventType.UPLOAD_RECORD == message.type) {
            mViewModel.getList(true)
        }
    }
}